package com.project.logistics.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.logistics.bean.*;
import com.project.logistics.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by94988 on 2021/3/19 16:08
 */
@Controller
@Slf4j
public class AdmController {


    @Autowired
    RepairsService repairsService;

    @Autowired
    UserService userService;

    @Autowired
    RepairmanService repairmanService;

    @Autowired
    UserDetailService userDetailService;

    @Autowired
    NoticeService noticeService;

    @Autowired
    EvaluateService evaluateService;

    @GetMapping("/adm/main")
    public String mainPage(){
//    public String mainPage(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model){
//        Page<Repairs> page = new Page<>(pn, 2);
//        Page<Repairs> repairsPage = repairsService.page(page, null);
//        model.addAttribute("RepairsPage",repairsPage);
        return "adm/main";
    }
    @GetMapping("/adm/checkRepairmans")
    public String checkRepairmans(@RequestParam(value = "pn", defaultValue = "1") Integer pn,Model model){
        Page<Repairman> page = new Page<>(pn, 5);
        Page<Repairman> repairmanPage = repairmanService.page(page, null);
        model.addAttribute("repairmanPage",repairmanPage);
        return "/adm/checkRepairmans";
    }


@GetMapping("/adm/checkRepairmanById")
public String checkRepairmanById(String userId,Model model){
    Repairman repairman = repairmanService.findRepairmanById(userId);
    model.addAttribute("repairman",repairman);
    return "adm/repairman-detail";
}

    @GetMapping("/adm/toAddRepairman")
    public String toAddRepairman(){
        return "/adm/addRepairman";
    }

    @PostMapping("/adm/addRepairman")
    public String addRepairman(Repairman repairman, MultipartFile headerImg, Model model) throws IOException {
        User user = new User(repairman.getUserId(), "666");
        user.setUserType(2);
        userService.save(user);
        String path = "E:\\javaee\\CODE\\logisticsSystem-v3\\src\\main\\resources\\static\\photos\\";
        if (!headerImg.isEmpty()) {
            //获取文件的原文件名
            String originalFilename = headerImg.getOriginalFilename();
            log.info("originalFilename={}", originalFilename);
            //封装的方法，把文件传输到哪个文件下

            headerImg.transferTo(new File(path + originalFilename));
            repairman.setRepairmanPhoto("\\photos\\" + originalFilename);
        }
        boolean flag = repairmanService.save(repairman);
        return "redirect:/adm/checkRepairmans";
    }

    @GetMapping("/adm/deleteRepairman")
    public String deleteRepairman(String deleteId,Model model){

        boolean flag1 = repairmanService.deleteById(deleteId);
        boolean flag2 = userService.deleteUserById(deleteId);
        return "redirect:/adm/checkRepairmans";
    }


    @GetMapping("/adm/checkUndealrepairs")
    public String getUndealrepairs(@RequestParam(value = "pn", defaultValue = "1") Integer pn,Model model){
        List<Repairs> undealrepairs = repairsService.getUndealrepairs();
        model.addAttribute("undealrepairs",undealrepairs);
        List<Repairman> repairmans = repairmanService.list();
        model.addAttribute("repairmans",repairmans);
        return "/adm/checkUndealrepairs";
    }

    @GetMapping("/adm/checkAllrepairs")
    public String checkAllrepairs(@RequestParam(value = "pn", defaultValue = "1") Integer pn,Model model){
        Page<Repairs> page = new Page<>(pn, 5);
        Page<Repairs> repairsPage = repairsService.page(page, null);
        model.addAttribute("repairsPage",repairsPage);
        return "/adm/checkAllrepairs";
    }

    @GetMapping("/adm/selectRepairman")
    public String selectRepairman(String repairmanId,int repairsId){
        repairsService.setRepairman(repairmanId,repairsId);
        repairmanService.setCount(repairmanId);
        return "redirect:/adm/checkUndealrepairs";

    }

    @GetMapping("/adm/toUpdateRepairman")
    public String toUpdateRepairman(String repairmanid, Model model){
        Repairman repairman = repairmanService.findRepairmanById(repairmanid);
        model.addAttribute("repairman",repairman);
        return "/adm/updateRepairman";
    }


    @GetMapping("/adm/updateRepairman")
    public String updateRepairman(Repairman repairman,Model model){
        int i = repairmanService.updateRepairman(repairman);
        System.out.println(repairman);
        if (i == 0){
            System.out.println("修改失败！");
        }
        return "redirect:/adm/checkRepairmans";
    }

    @GetMapping("/adm/toAddUsers")
    public String toAddUser(){
        return "/adm/addUser";
    }

    @GetMapping("/adm/addUsers")
    public String addUSers(String prefix,int start,int end){
        User user = new User();
        for (int i = start; i <= end; i++) {
            user.setUserId(prefix+i);
            user.setPassword("666");
            user.setUserType(1);
            userService.save(user);
        }
        return "redirect:/adm/checkUserByType";
    }

    @GetMapping("/adm/checkUserByType")
    public  String checkUserByType(@RequestParam(value = "type",defaultValue = "1") int type , Model model){
        List<User> users = userService.getUserByType(type);
        for (User user : users) {
            user.setTypeStr("普通用户");
        }
        model.addAttribute("users",users);
        return "/adm/checkUsers";
    }

    @GetMapping("/adm/deleteUserById")
    public String deleteUserById(String userId){
        userService.deleteUserById(userId);
        return "redirect:/adm/checkUserByType";
    }

    @GetMapping("/adm/resetUserPassword")
    public String resetUserPassword(String userId){
        userService.resetPassword(userId);
        return "redirect:/adm/checkUserByType";

    }

    @GetMapping("/adm/resetRepairmanPassword")
    public String resetRepairmanPassword(String userId){
        userService.resetPassword(userId);
        return "redirect:/adm/checkRepairmans";

    }

    @GetMapping("/adm/checkNotices")
    public String checkNotices(@RequestParam(value = "pn", defaultValue = "1") Integer pn,Model model){
        Page<Notice> page = new Page<>(pn, 2);
        Page<Notice> noticePage = noticeService.page(page,null);
        model.addAttribute("noticePage",noticePage);
        return "/adm/checkNotices";
    }

    @GetMapping("/adm/deleteNotice")
    public String deleteNotice(int noticeId){
//        System.out.println("我进来了");
        noticeService.deleteNotice(noticeId);
        return "redirect:/adm/checkNotices";
    }



    @GetMapping("/adm/toUpdateNotice")
    public String toUpdateNotice(int noticeId,Model model){
        Notice notice = noticeService.findNoticeById(noticeId);
        model.addAttribute("notice",notice);
        return "/adm/updateNotice";
    }

    @GetMapping("/adm/updateNotice")
    public String updateNotice(Notice notice,Model model){
        noticeService.updateRepairman(notice);
        return "redirect:/adm/checkNotices";
    }

    @GetMapping("/adm/toAddNotice")
    public String toAddNotice(){
        return "/adm/addNotice";
    }

    @GetMapping("/adm/addNotice")
    public String addNotice(Notice notice){
        noticeService.save(notice);
        return "redirect:/adm/checkNotices";
    }

    @GetMapping("/adm/toRepairDetail")
    public String toRepairDetail(int repairsId,Model model){
        Repairs repair = repairsService.getRepairById(repairsId);
        UserDetail user = userDetailService.getUserDetailById(repair.getRepairsUserId());
        Repairman repairman = repairmanService.findRepairmanById(repair.getServiceUserId());
        Evaluate evaluate = evaluateService.fingEvaluateById(repair.getRepairsId());
        if ( evaluate ==null){
            evaluate = new Evaluate();
        }
        if (repairman == null ){
            repairman = new Repairman();
        }
        List<Repairman> repairmans = repairmanService.list();
        model.addAttribute("repairmans",repairmans);
        model.addAttribute("repair",repair);
        System.out.println(repair);
        model.addAttribute("user",user);
        model.addAttribute("repairman",repairman);
        System.out.println(repairman);
        model.addAttribute("evaluate",evaluate);
        return "/adm/repair-detail";
    }



    @GetMapping("/adm/deleteComment")
    public String deleteComment(int repairsId){
        evaluateService.deleteComment(repairsId);
        return "redirect:/adm/checkAllrepairs";
    }

    @GetMapping("/adm/deleteAnswer")
    public String deleteAnswer(int repairsId){
        evaluateService.deleteAnswer(repairsId);
        return "redirect:/adm/checkAllrepairs";
    }

    @GetMapping("/adm/deleteRepairs")
    public String deleteRepairs(int repairsId){
        repairsService.deleteRepairs(repairsId);
        return "redirect:/adm/checkAllrepairs";
    }


    @GetMapping("/adm/setTodayRepairman")
    public String setTodayRepairman(String userId){
        Repairman todayRepairman = repairmanService.getTodayRepairman();
        repairmanService.deleteTodayRepairman(todayRepairman.getUserId());
        repairmanService.updateTodayRepairman(userId);
        return "redirect:/adm/checkRepairmans";
    }
}
